﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MinimizeEngine.Components.Render
{
    /// <summary>
    /// Provides a set of methods for rendering Rays.
    /// </summary>
    public static class RayRenderer
    {
        static VertexPositionColor[] verts = new VertexPositionColor[2];

        static VertexPositionColor[] arrowVerts = new VertexPositionColor[]
        {
            new VertexPositionColor(Vector3.Zero, Color.White),
            new VertexPositionColor(new Vector3(2.5f, 0f, -2.5f), Color.Black),
            new VertexPositionColor(new Vector3(-2.5f, 0f, -2.5f), Color.Black),
            new VertexPositionColor(new Vector3(0f, 2.5f, -2.5f), Color.Black),
            new VertexPositionColor(new Vector3(0f, -2.5f, -2.5f), Color.Black),
        };
        static int[] arrowIndexs = new int[]
        {
            0, 1,
            0, 2,
            0, 3,
            0, 4,
        };

        static VertexDeclaration vertDecl;
        static BasicEffect effect;

        /// <summary>
        /// Renders a Ray for debugging purposes.
        /// </summary>
        /// <param name="ray">The ray to render.</param>
        /// <param name="length">The distance along the ray to render.</param>
        /// <param name="graphicsDevice">The graphics device to use when rendering.</param>
        /// <param name="view">The current view matrix.</param>
        /// <param name="projection">The current projection matrix.</param>
        /// <param name="color">The color to use drawing the ray.</param>
        public static void Render(
            Ray ray,
            float length,
            GraphicsDevice graphicsDevice,
            Matrix view,
            Matrix projection,
            Color color)
        {
            if (effect == null)
            {
                effect = new BasicEffect(graphicsDevice, null);
                effect.VertexColorEnabled = false;
                effect.LightingEnabled = false;
                vertDecl = new VertexDeclaration(graphicsDevice, VertexPositionColor.VertexElements);
            }

            verts[0] = new VertexPositionColor(ray.Position, Color.White);
            verts[1] = new VertexPositionColor(ray.Position + (ray.Direction * length), Color.White);

            effect.DiffuseColor = color.ToVector3();
            effect.Alpha = (float)color.A / 255f;

            effect.World = Matrix.Identity;
            effect.View = view;
            effect.Projection = projection;

            graphicsDevice.VertexDeclaration = vertDecl;

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, verts, 0, 1);

                effect.World = Matrix.Invert(Matrix.CreateLookAt(
                    verts[1].Position,
                    verts[0].Position,
                    (ray.Direction != Vector3.Up) ? Vector3.Up : Vector3.Left));
                effect.CommitChanges();

                graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, arrowVerts, 0, 5, arrowIndexs, 0, 4);

                pass.End();
            }
            effect.End();
        }
    }
}
